package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmBusinessService;
import com.mdd.admin.validate.ClientCrmBusinessCreateValidate;
import com.mdd.admin.validate.ClientCrmBusinessUpdateValidate;
import com.mdd.admin.validate.ClientCrmBusinessSearchValidate;
import com.mdd.admin.vo.ClientCrmBusinessListedVo;
import com.mdd.admin.vo.ClientCrmBusinessDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmBusiness;
import com.mdd.common.mapper.ClientCrmBusinessMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 商机实现类
 * @author LikeAdmin
 */
@Service
public class ClientCrmBusinessServiceImpl implements IClientCrmBusinessService {
        
    @Resource
    ClientCrmBusinessMapper clientCrmBusinessMapper;

    /**
     * 商机列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmBusinessListedVo>
     */
    @Override
    public PageResult<ClientCrmBusinessListedVo> list(PageValidate pageValidate, ClientCrmBusinessSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmBusinessMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:businessName@business_name:str",
            "=:privateCustomerCustomer@private_customer_customer:str",
            "between:estimatedSigningDate@estimated_signing_date:str",
            "=:signingPossibility@signing_possibility:str",
            "=:fieldStaffId@field_staff_id:int",
            "=:collaboratorId@collaborator_id:int",
            "=:userId@user_id:int",
        });

        IPage<ClientCrmBusiness> iPage = clientCrmBusinessMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmBusinessListedVo> list = new LinkedList<>();
        for(ClientCrmBusiness item : iPage.getRecords()) {
            ClientCrmBusinessListedVo vo = new ClientCrmBusinessListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 商机详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCrmBusiness
     */
    @Override
    public ClientCrmBusinessDetailVo detail(Integer id) {
        ClientCrmBusiness model = clientCrmBusinessMapper.selectOne(
                new QueryWrapper<ClientCrmBusiness>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmBusinessDetailVo vo = new ClientCrmBusinessDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 商机新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmBusinessCreateValidate createValidate) {
        ClientCrmBusiness model = new ClientCrmBusiness();
        model.setBusinessName(createValidate.getBusinessName());
        model.setPrivateCustomerCustomer(createValidate.getPrivateCustomerCustomer());
        model.setEstimatedSalesAmount(createValidate.getEstimatedSalesAmount());
        model.setEstimatedSigningDate(createValidate.getEstimatedSigningDate());
        model.setDepartment(createValidate.getDepartment());
        model.setSigningPossibility(createValidate.getSigningPossibility());
        model.setMainOwningCompanyid(createValidate.getMainOwningCompanyid());
        model.setSubsidiaryCompanyId(createValidate.getSubsidiaryCompanyId());
        model.setUpdatedOn(createValidate.getUpdatedOn());
        model.setBusinessType(createValidate.getBusinessType());
        model.setBusinessGetdate(createValidate.getBusinessGetdate());
        model.setBusinessSource(createValidate.getBusinessSource());
        model.setDefaultPoint(createValidate.getDefaultPoint());
        model.setCaseType(createValidate.getCaseType());
        model.setPauseName(createValidate.getPauseName());
        model.setTechnicalPoints(createValidate.getTechnicalPoints());
        model.setAbstractText(createValidate.getAbstractText());
        model.setBusinessSourceid(createValidate.getBusinessSourceid());
        model.setCaseTypeid(createValidate.getCaseTypeid());
        model.setFieldStaffId(createValidate.getFieldStaffId());
        model.setCollaboratorId(createValidate.getCollaboratorId());
        model.setRemarks(createValidate.getRemarks());
        model.setFollowupStatus(createValidate.getFollowupStatus());
        model.setNextTime(createValidate.getNextTime());
        model.setUserId(createValidate.getUserId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        clientCrmBusinessMapper.insert(model);
    }

    /**
     * 商机编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmBusinessUpdateValidate updateValidate) {
        ClientCrmBusiness model = clientCrmBusinessMapper.selectOne(
                new QueryWrapper<ClientCrmBusiness>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setBusinessName(updateValidate.getBusinessName());
        model.setPrivateCustomerCustomer(updateValidate.getPrivateCustomerCustomer());
        model.setEstimatedSalesAmount(updateValidate.getEstimatedSalesAmount());
        model.setEstimatedSigningDate(updateValidate.getEstimatedSigningDate());
        model.setDepartment(updateValidate.getDepartment());
        model.setSigningPossibility(updateValidate.getSigningPossibility());
        model.setMainOwningCompanyid(updateValidate.getMainOwningCompanyid());
        model.setSubsidiaryCompanyId(updateValidate.getSubsidiaryCompanyId());
        model.setUpdatedOn(updateValidate.getUpdatedOn());
        model.setBusinessType(updateValidate.getBusinessType());
        model.setBusinessGetdate(updateValidate.getBusinessGetdate());
        model.setBusinessSource(updateValidate.getBusinessSource());
        model.setDefaultPoint(updateValidate.getDefaultPoint());
        model.setCaseType(updateValidate.getCaseType());
        model.setPauseName(updateValidate.getPauseName());
        model.setTechnicalPoints(updateValidate.getTechnicalPoints());
        model.setAbstractText(updateValidate.getAbstractText());
        model.setBusinessSourceid(updateValidate.getBusinessSourceid());
        model.setCaseTypeid(updateValidate.getCaseTypeid());
        model.setFieldStaffId(updateValidate.getFieldStaffId());
        model.setCollaboratorId(updateValidate.getCollaboratorId());
        model.setRemarks(updateValidate.getRemarks());
        model.setFollowupStatus(updateValidate.getFollowupStatus());
        model.setNextTime(updateValidate.getNextTime());
        model.setUserId(updateValidate.getUserId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        clientCrmBusinessMapper.updateById(model);
    }

    /**
     * 商机删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmBusiness model = clientCrmBusinessMapper.selectOne(
                new QueryWrapper<ClientCrmBusiness>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        clientCrmBusinessMapper.updateById(model);
    }

}
